<template>
  <div class="app-container">
    <t-form
      :model="queryParams"
      ref="queryRef"
      layout="inline"
      v-show="showSearch"
      label-width="68px"
      class="mb20"
    >
      <t-form-item label="登录地址" name="ipaddr">
        <t-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 240px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="用户名称" name="userName">
        <t-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="状态" name="status">
        <t-select
          v-model="queryParams.status"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <t-option
            v-for="dict in sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item label="登录时间" style="width: 308px">
        <t-date-range-picker
          v-model="dateRange"
          enable-time-picker
          allow-input
          clearable
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:logininfor:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          @click="handleClean"
          v-hasPermi="['monitor:logininfor:remove']"
          >清空
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="primary"
          :disabled="single"
          @click="handleUnlock"
          v-hasPermi="['monitor:logininfor:unlock']"
          >解锁
          <template #icon>
            <t-icon name="lock-off" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="warning"
          @click="handleExport"
          v-hasPermi="['monitor:logininfor:export']"
          >导出
          <template #icon>
            <t-icon name="download" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      ref="logininforRef"
      v-loading="loading"
      :data="logininforList"
      row-key="infoId"
      :columns="columns"
      @select-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <template #status="scope">
        <dict-tag :options="sys_common_status" :value="scope.row.status" />
      </template>
      <template #loginTime="scope">
        <span>{{ parseTime(scope.row.loginTime) }}</span>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import {
  list,
  delLogininfor,
  cleanLogininfor,
  unlockLogininfor,
} from "@/api/monitor/logininfor";

const { proxy } = getCurrentInstance();
const { sys_common_status } = proxy.useDict("sys_common_status");

const logininforList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const selectName = ref("");
const total = ref(0);
const dateRange = ref([]);
const defaultSort = ref({ sortBy: "loginTime", descending: "true" });

const columns = ref([
  { colKey: "row-select", type: "multiple", width: 50, align: "center" },
  { colKey: "infoId", title: "访问编号", width: 100, align: "center" },
  {
    colKey: "userName",
    title: "用户名称",
    align: "center",
    ellipsis: true,
    sorter: true,
  },
  {
    colKey: "ipaddr",
    title: "地址",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "loginLocation",
    title: "登录地址",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "os",
    title: "操作系统",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "browser",
    title: "浏览器",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "status",
    title: "登录状态",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "msg",
    title: "描述",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "loginTime",
    title: "访问时间",
    align: "center",
    width: 180,
    ellipsis: true,
    sorter: true,
  },
]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  ipaddr: undefined,
  userName: undefined,
  status: undefined,
  orderByColumn: undefined,
  isAsc: undefined,
});

/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      logininforList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.orderByColumn = defaultSort.sortBy;
  queryParams.isAsc = defaultSort.descending ? "desc" : "asc";
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  ids.value = selectedRowKeys;
  single.value = selectedRowKeys.length != 1;
  multiple.value = !selectedRowKeys.length;
  selectName.value = selectedRowData.map((item) => item.userName);
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.sortBy;
  queryParams.value.isAsc = column.descending ? "desc" : "asc";
  getList();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const infoIds = row.infoId || ids.value;
  proxy.$modal
    .confirm('是否确认删除访问编号为"' + infoIds + '"的数据项?')
    .then(function () {
      return delLogininfor(infoIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有登录日志数据项?")
    .then(function () {
      return cleanLogininfor();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}
/** 解锁按钮操作 */
function handleUnlock() {
  const username = selectName.value;
  proxy.$modal
    .confirm('是否确认解锁用户"' + username + '"数据项?')
    .then(function () {
      return unlockLogininfor(username);
    })
    .then(() => {
      proxy.$modal.msgSuccess("用户" + username + "解锁成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "monitor/logininfor/export",
    {
      ...queryParams.value,
    },
    `config_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
